package com.zwf.checkstyledemo1.test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class InstallerWizard extends JFrame {
    int x = 1, y = 0;
    int res = x / y;

    // 步骤标题
    private final String[] steps = {
            "欢迎",
            "许可协议",
            "安装位置",
            "安装中",
            "完成"
    };

    private int currentStep = 0;
    private JPanel cardsPanel;
    private CardLayout cardLayout;
    private JButton backButton, nextButton;
    private JProgressBar progressBar;

    public InstallerWizard() {
        setTitle("应用程序安装向导");
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setLayout(new BorderLayout(10, 10));

        // 创建顶部步骤指示器
        add(createStepIndicator(), BorderLayout.NORTH);

        // 创建卡片面板用于多步骤内容
        cardLayout = new CardLayout();
        cardsPanel = new JPanel(cardLayout);
        createCards();
        add(cardsPanel, BorderLayout.CENTER);

        // 创建底部按钮面板
        add(createButtonPanel(), BorderLayout.SOUTH);

        updateButtonState();
        setVisible(true);
    }

    private JPanel createStepIndicator() {
        JPanel panel = new JPanel(new GridLayout(1, steps.length, 5, 5));
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        panel.setBackground(new Color(240, 240, 240));

        for (int i = 0; i < steps.length; i++) {
            JLabel stepLabel = new JLabel(steps[i], SwingConstants.CENTER);
            stepLabel.setOpaque(true);
            stepLabel.setBorder(BorderFactory.createLineBorder(Color.GRAY));

            if (i == currentStep) {
                stepLabel.setBackground(new Color(180, 220, 255));
                stepLabel.setFont(stepLabel.getFont().deriveFont(Font.BOLD));
            } else {
                stepLabel.setBackground(Color.WHITE);
            }

            panel.add(stepLabel);
        }
        return panel;
    }

    private void createCards() {
        // 步骤1: 欢迎
        JPanel welcomePanel = new JPanel(new BorderLayout());
        welcomePanel.add(new JLabel(
                "<html><div style='text-align: center; padding: 30px;'>" +
                        "<h1>欢迎使用安装向导</h1>" +
                        "<p>此向导将引导您完成应用程序的安装过程</p></div></html>",
                SwingConstants.CENTER), BorderLayout.CENTER);

        // 步骤2: 许可协议
        JPanel licensePanel = new JPanel(new BorderLayout());
        JTextArea licenseArea = new JTextArea("这里是软件许可协议内容...\n\n请仔细阅读许可条款。");
        licenseArea.setEditable(false);
        licensePanel.add(new JScrollPane(licenseArea), BorderLayout.CENTER);
        JCheckBox agreeCheck = new JCheckBox("我接受许可协议");
        licensePanel.add(agreeCheck, BorderLayout.SOUTH);

        // 步骤3: 安装位置
        JPanel locationPanel = new JPanel(new BorderLayout());
        locationPanel.add(new JLabel("选择安装位置:", SwingConstants.LEFT), BorderLayout.NORTH);
        JPanel inputPanel = new JPanel(new BorderLayout());
        JTextField pathField = new JTextField("C:\\Program Files\\MyApp");
        JButton browseButton = new JButton("浏览...");
        inputPanel.add(pathField, BorderLayout.CENTER);
        inputPanel.add(browseButton, BorderLayout.EAST);
        locationPanel.add(inputPanel, BorderLayout.CENTER);

        // 步骤4: 安装进度
        JPanel installPanel = new JPanel(new BorderLayout());
        installPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        progressBar = new JProgressBar(0, 100);
        progressBar.setStringPainted(true);
        installPanel.add(new JLabel("正在安装，请稍候...", SwingConstants.CENTER), BorderLayout.NORTH);
        installPanel.add(progressBar, BorderLayout.CENTER);

        // 步骤5: 完成
        JPanel finishPanel = new JPanel(new BorderLayout());
        finishPanel.add(new JLabel(
                "<html><div style='text-align: center; padding: 30px;'>" +
                        "<h1>安装完成</h1>" +
                        "<p>应用程序已成功安装到您的计算机上</p></div></html>",
                SwingConstants.CENTER), BorderLayout.CENTER);
        JCheckBox launchCheck = new JCheckBox("启动应用程序", true);
        finishPanel.add(launchCheck, BorderLayout.SOUTH);

        // 添加所有卡片
        cardsPanel.add(welcomePanel, steps[0]);
        cardsPanel.add(licensePanel, steps[1]);
        cardsPanel.add(locationPanel, steps[2]);
        cardsPanel.add(installPanel, steps[3]);
        cardsPanel.add(finishPanel, steps[4]);
    }

    private JPanel createButtonPanel() {
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 10));
        backButton = new JButton("上一步");
        nextButton = new JButton("下一步");
        JButton cancelButton = new JButton("取消");

        // 按钮事件处理
        backButton.addActionListener(e -> navigate(-1));
        nextButton.addActionListener(e -> navigate(1));
        cancelButton.addActionListener(e -> System.exit(0));

        panel.add(backButton);
        panel.add(nextButton);
        panel.add(cancelButton);

        return panel;
    }

    private void navigate(int direction) {
        // 模拟安装过程
        if (currentStep == 3 && direction == 1) {
            simulateInstallation();
            return;
        }

        currentStep += direction;
        cardLayout.show(cardsPanel, steps[currentStep]);
        updateButtonState();

        // 更新步骤指示器
        getContentPane().remove(0); // 移除旧的指示器
        getContentPane().add(createStepIndicator(), BorderLayout.NORTH);
        revalidate();
        repaint();
    }

    private void simulateInstallation() {
        new Thread(() -> {
            nextButton.setEnabled(false);
            backButton.setEnabled(false);

            for (int i = 0; i <= 100; i++) {
                try {
                    Thread.sleep(30); // 模拟安装延迟
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                final int progress = i;
                SwingUtilities.invokeLater(() -> {
                    progressBar.setValue(progress);
                    progressBar.setString("安装进度: " + progress + "%");
                });
            }

            SwingUtilities.invokeLater(() -> {
                currentStep++;
                cardLayout.show(cardsPanel, steps[currentStep]);
                updateButtonState();
                getContentPane().remove(0);
                getContentPane().add(createStepIndicator(), BorderLayout.NORTH);
                revalidate();
                repaint();
            });
        }).start();
    }

    private void updateButtonState() {
        backButton.setEnabled(currentStep > 0);

        if (currentStep == steps.length - 1) {
            nextButton.setText("完成");
            nextButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(InstallerWizard.this, "安装完成！");
                    System.exit(0);
                }
            });
        } else if (currentStep == 3) {
            nextButton.setText("安装");
        } else {
            nextButton.setText("下一步");
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new InstallerWizard());
    }
}
